#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

from PySide import QtGui, QtCore
from .pyuic.list_item_widget import Ui_list_item_widget
import uuid
import os
from functools import partial
from .get_path import get_folder_path

class ListItemWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(ListItemWidget, self).__init__(parent)
        self._parent = parent

        self._ui = Ui_list_item_widget()
        self._ui.setupUi(self)
        self.name = uuid.uuid4().hex
        self._menu = QtGui.QMenu()
        self._ui.version_action_btn.setMenu(self._menu)
        self._ui.frame.setObjectName(self.name)
        self._ui.package_btn.setVisible(0)
        self._hover_style = '''
            *{border:None;background-color:rgb(0,0,0,0);}
            QFrame::hover#%s{background-color:rgb(0,0,0,15);}
            ''' % self.name
        self._sel_style = '''
            *{border:None;background-color:rgb(0,0,0,0);}
            QFrame#%s{background-color:rgb(0,0,0,25);}
            ''' % self.name
        self._ui.frame.mousePressEvent = self.mouse_press_event
        # self._ui.engine_icon.mouseDoubleClickEvent = self.on_engine_double_clicked
        self._ui.frame.mouseDoubleClickEvent = self.on_engine_item_double_clicked
        self.set_hover_style()
        self.pro = []
        self._ui.version_action_btn.setStyleSheet("QPushButton::menu-indicator{image:none;}")

    def _create_action(self,engine, actions):
        for name in actions:
            icon_path = os.path.join(get_folder_path('icons'), engine + str(name) + '.png')
            action = QtGui.QAction(str(name), self)
            action.setIcon(QtGui.QIcon(icon_path))
            action.triggered.connect(partial(self._set_version, str(name)))
            self._menu.addAction(action)

    def set_config(self,group_name, data):
        cmd = data.get('cmd')
        if not cmd:
            return
        self._engine_package = data.get('engine')
        engine_name = data.get('name')
        default_version = data.get('default')
        versions = data.get('versions')
        packages = data.get('packages', '')
        icon_path = os.path.join(get_folder_path('icons'), engine_name + '.png')
        if versions:
            if default_version:
                current_version = str(default_version)
            else:
                current_version = str(versions[0])
            self._set_version(current_version)
            self._create_action(engine_name, versions)
            icon_path = os.path.join(get_folder_path('icons'), engine_name +current_version+'.png')

        self.cmd = """rez-env %s {engine} -- %s""" % (' '.join(packages), cmd)
        self._set_name(engine_name)
        self._set_icon(icon_path)

    def _set_name(self, text):
        self._ui.engine_name.setText(text)

    def _set_version(self, version):
        self._ui.version_label.setText(version)
        engine_name = self._ui.engine_name.text()
        icon_path = os.path.join(get_folder_path('icons'), engine_name+version+'.png')
        if not os.path.isfile(icon_path):
            icon_path = os.path.join(get_folder_path('icons'), engine_name+'.png')
        self._set_icon(icon_path)

    def _set_icon(self, pixmap):
        self._ui.engine_icon.setPixmap(pixmap)
        self._ui.engine_icon.setVisible(1)
        self._ui.version_action_btn.setIcon(QtGui.QIcon(pixmap))
        self._ui.version_action_btn.setVisible(0)

    def set_hover_style(self):
        self.setStyleSheet(self._hover_style)

    def set_package_btn_show(self, state):
        self._ui.package_btn.setVisible(state)

    def mouse_press_event(self, *args):
        if self._parent.current_item:
            self._parent.current_item.set_hover_style()
            self._parent.current_item._set_action_show(0)
            self._parent.current_item.set_package_btn_show(0)
        self._parent.current_item = self
        self.setStyleSheet(self._sel_style)
        self.set_package_btn_show(1)
        self._set_action_show(1)

    def _set_action_show(self, statue):
        if statue:
            self._ui.engine_icon.setHidden(1)
            self._ui.version_action_btn.setHidden(0)
        else:
            self._ui.engine_icon.setHidden(0)
            self._ui.version_action_btn.setHidden(1)

    def exec_cmd(self, cmd):
        print cmd
        QtCore.QProcess.startDetached(cmd)

    def on_engine_item_double_clicked(self, *args):
        engine_name = self._engine_package
        if not self._engine_package:
            engine_name = self._ui.engine_name.text()
        version_name = self._ui.version_label.text()
        if version_name:
            engine_package = engine_name + '-' + version_name
        else:
            engine_package = engine_name
        cmd = self.cmd.format(engine = engine_package)
        self.exec_cmd(cmd)






